﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.ObjectBuilder;
using Metro.Admin.Views;

public partial class Admin_Credit : Microsoft.Practices.CompositeWeb.Web.UI.Page, ICredit
{
    private CreditPresenter _presenter;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this._presenter.OnViewInitialized();
        }
        this._presenter.OnViewLoaded();
    }

    [CreateNew]
    public CreditPresenter Presenter
    {
        set
        {
            this._presenter = value;
            this._presenter.View = this;
        }
    }

    public void ShowCredits(System.Collections.Generic.List<Metro.Admin.BusinessEntities.Credit> credits)
    {
        CreditContainerDataSource.DataSource = credits;
        CreditGridView.Visible = true;  
    }

    public void ShowCustomers(System.Collections.Generic.List<Metro.Admin.BusinessEntities.Customer> customers)
    {
        CustomerContainerDataSource.DataSource = customers;   
    }

    public void DeleteCredit(Metro.Admin.BusinessEntities.Credit credit)
    {
        this._presenter.DeleteCredit(credit);
    }

    public void CreateCredit(Metro.Admin.BusinessEntities.Credit credit) 
    {
        this._presenter.CreateCredit(credit);
    }

    // TODO: Forward events to the presenter and show state to the user.
    // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
    //		ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.practices.wcsf.2007jun/wcsf/html/02-480-ViewPresenter.htm

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            this.AssignCredit();
            this._presenter.OnViewLoaded();
        }
        catch (Exception ex)
        {
            Trace.Warn("Credit.aspx - btnAdd_Click", ex.Message);   
        }
    }

    private void AssignCredit()
    {
        try
        {
            
            Metro.Admin.BusinessEntities.Credit credit = new Metro.Admin.BusinessEntities.Credit();

            credit.IdCustomer = ddlCustomer.SelectedItem.Value;
            credit.Type = ddlCreditType.SelectedItem.Value;
            credit.Amount = decimal.Parse(txtAmount.Text);

            this.CreateCredit(credit); 
            
            txtAmount.Text = string.Empty;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void CreditContainerDataSource_Deleted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.DeleteCredit((Metro.Admin.BusinessEntities.Credit)e.Instance);
        this._presenter.OnViewLoaded();
    }
}


